<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="108px">
      <el-form-item label="通知类型" prop="type">
        <el-select v-model="queryParams.type" clearable placeholder="请选择通知类型">
          <el-option
            v-for="dict in noticeType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            size="small"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="通知标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入通知标题"
          maxlength="25"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="createTime">
        <el-date-picker
          v-model="createTime"
          size="small"
          style="width: 240px"
          :editable="false"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="noticeList">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通知类型" align="center" prop="type">
        <template slot-scope="scope">
          <span
            v-html="scope.row.type == 0 ? '通知' : scope.row.type == 1 ? '提醒' : ''"
          ></span>
        </template>
      </el-table-column>
      <el-table-column
        label="通知标题"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="通知范围" align="center" prop="scope">
        <template slot-scope="scope">
          <span
            v-html="scope.row.scope == 0 ? '班级' : scope.row.scope == 1 ? '学校' : ''"
          ></span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="发布时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:student:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="通知类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择通知类型">
            <el-option
              v-for="dict in noticeType"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              size="small"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通知范围" prop="scope">
          <el-select
            v-model="form.scope"
            placeholder="请选择通知范围"
            @change="getclassList"
            :disabled="ydflag"
          >
            <el-option
              v-for="dict in noticeScope"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              size="small"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <input
          v-if="form.scope == '1'"
          type="hidden"
          v-model="form.schoolCode == schoolCode"
        />
        <el-form-item label="选择班级" prop="classCode" v-if="form.scope == '0'">
          <el-select v-model="form.classCode" placeholder="请选择班级" :disabled="ydflag">
            <el-option
              v-for="item in classList"
              :key="item.id"
              :label="item.label"
              :value="item.id"
              size="small"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通知标题" prop="title">
          <el-input
            v-model="form.title"
            placeholder="请输入通知标题"
            clearable
            maxlength="25"
            size="small"
          />
        </el-form-item>
        <el-form-item label="通知对象" prop="target">
          <el-select v-model="form.target" :disabled="ydflag" placeholder="请选择通知对象">
            <el-option
              v-for="dict in targetType"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              size="small"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通知内容" prop="content">
          <editor v-model="form.content" :min-height="192" />
          <!--          <el-input v-model="form.content" placeholder="请输入通知内容"-->
          <!--                    type="textarea"-->
          <!--                    clearable-->
          <!--                    style=""-->
          <!--                    size="small"/>-->
        </el-form-item>
        <!--        <el-form-item label="发布时间" prop="createTime">-->
        <!--          <el-date-picker clearable size="small" style="width: 200px"-->
        <!--                          v-model="form.createTime"-->
        <!--                          type="datetime"-->
        <!--                          value-format="yyyy-MM-dd HH:mm:ss"-->
        <!--                          placeholder="选择开始使用时间">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addNoticeList,
  getNoticedetailList,
  getNoticeList,
  updateNoticeList,
  delNoticeList,
} from "@/api/base/notice";
import { selectClazz } from "@/api/base/clazz";
import { addStudent, updateStudent } from "@/api/base/student";
import Editor from "@/components/Editor";
import Treeselect from "@riophae/vue-treeselect";

export default {
  name: "gzt",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type:[
          { required: true, message: '请选择通知类型', trigger: 'change' },
        ],
        target:[
          { required: true, message: '请选择通知对象', trigger: 'change' },
        ],
        scope:[
          { required: true, message: '请选择通知范围', trigger: 'change' },
        ],
        classCode:[
          { required: true, message: '请选择班级', trigger: 'blur' },
        ],
        title:[
          { required: true, message: '请输入通知标题', trigger: 'blur' },
        ],
        content:[
          { required: true, message: '请输入通知内容', trigger: 'change' },
        ],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      schoolCode: this.$store.getters.schoolCode,
      noticeType: [
        { label: "通知", value: 0 },
        { label: "提醒", value: 1 },
      ], //通知类型
      targetType: [
        { label: "全部", value: 0 },
        { label: "家长", value: 1 },
        { label: "教职工", value: 2 },
      ], //通知对象
      noticeScope: [
        { label: "班级", value: "0" },
        { label: "学校", value: "1" },
      ], //通知范围
      noticeList: [],
      classList: [],
      ydflag: false, //通知范围和班级  新增可以更改  修改不可以
      createTime: [],
    };
  },
  created() {
    this.getList();
    this.getclassList();
  },
  methods: {
    /**获取列表**/
    async getList() {
      try {
        this.loading = true;
        const data = await getNoticeList(this.queryParams);
        this.noticeList = data.rows;
        this.total = data.total;
        this.loading = false;
      } catch (e) {
        console.log(e);
      }
    },
    /**班级*/
    async getclassList() {
      try {
        const { rows } = await selectClazz();
        this.classList = rows.map((item) => {
          return {
            id: item.id + "",
            label: item.title,
          };
        });
      } catch (e) {
        console.log(e);
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$confirm('是否确认删除通知公告id为"' + id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delNoticeList(id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /**修改**/
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      //
      this.ydflag = true;
      getNoticedetailList(id).then((res) => {
        if (res.code == 200) {
          this.open = true;
          this.title = "修改通知提醒";
          this.form = res.data;
        } else {
          this.msgError(res.msg);
        }
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      //
      this.ydflag = false;
      this.title = "新增通知提醒";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateNoticeList(this.form).then((response) => {
              if (response.code == 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addNoticeList(this.form).then((response) => {
              if (response.code == 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        classCode: null,
        content: null,
        schoolCode: null,
        scope: null,
        title: null,
        type: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.createTime.length == 2) {
        let params = {
          beginTime: this.createTime[0],
          endTime: this.createTime[1],
        };
        this.queryParams.params = params;
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.createTime = [];
      this.queryParams.params = {};
      this.handleQuery();
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body .el-textarea__inner {
  min-height: 200px !important;
}
</style>
